package com.carrot.newsgallery.controller;

import com.carrot.newsgallery.common.R;
import com.carrot.newsgallery.service.NewsChildService;
import com.carrot.newsgallery.utils.NewsChildTableNameParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(description = "新闻子榜")
@RestController
@RequestMapping("/newschild")
public class NewsChildController {

    @Autowired
    private NewsChildService newsChildService;

    @GetMapping("{page}/{part}")
    @ApiOperation("查询榜单今日内容")
    public R getToday(@PathVariable int page,@PathVariable int part){
        String tablename = "news_child_" + Integer.toString(page)+"_"+Integer.toString(part);

        //设置查询的目标表名
        NewsChildTableNameParser.setIdentity(tablename);

        return R.success(newsChildService.getToday(tablename),"查询成功");
    }

    /**
     * date满足基本格式,batch为0-3中的数字
     */
    @GetMapping("{page}/{part}/{date}/{batch}")
    public R getHistory(@PathVariable int page,@PathVariable int part,
                        @PathVariable String date, @PathVariable int batch){
        String tablename = "news_child_" + Integer.toString(page)+"_"+Integer.toString(part);

        //设置查询的目标表名
        NewsChildTableNameParser.setIdentity(tablename);

        return R.success(newsChildService.getHistory(date,batch,tablename),"查询成功");
    }
}
